﻿using System;
using System.Linq.Expressions;

namespace BestEasyCMS.Framework.Querying
{
    /// <summary>
    /// 标准查询条件
    /// </summary>
    public class Criterion
    {
        private readonly string _propertyName;
        private readonly object _value;
        private readonly CriteriaOperator _criteriaOperator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="propertyName">属性名</param>
        /// <param name="value">值</param>
        /// <param name="criteriaOperator">操作符</param>
        public Criterion(string propertyName, object value, CriteriaOperator criteriaOperator)
        {
            _propertyName = propertyName;
            _value = value;
            _criteriaOperator = criteriaOperator;
        }
        /// <summary>
        /// 属性名
        /// </summary>
        public string PropertyName
        {
            get { return _propertyName; }
        }
        /// <summary>
        /// 值
        /// </summary>
        public object Value
        {
            get { return _value; }
        }
        /// <summary>
        /// 标准操作符
        /// </summary>
        public CriteriaOperator CriteriaOperator
        {
            get { return _criteriaOperator; }
        }
        /// <summary>
        /// 创建查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        /// <param name="value"></param>
        /// <param name="criteriaOperator"></param>
        /// <returns></returns>
        public static Criterion Create<T>(Expression<Func<T, object>> expression, object value, CriteriaOperator criteriaOperator)
        {
            var propertyName = PropertyNameHelper.ResolvePropertyName<T>(expression);
            var myCriterion = new Criterion(propertyName, value, criteriaOperator);
            return myCriterion;
        }
    }
}
